package com.swordget.controller;

import com.swordget.entity.vo.PanelVo;
import com.swordget.entity.vo.SimplePageResponseVo;
import com.swordget.entity.vo.SimpleRequestVo;
import com.swordget.entity.vo.SimpleResponseVo;
import com.swordget.exception.VerifyFailException;
import com.swordget.service.PanelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: David
 * @Date: Created in 10:45 2021/2/18
 * @Description: 看板
 * @Modified By:
 * @Version:
 */

@RestController
@RequestMapping(value = "/panel")
public class PanelController {

    @Autowired
    private PanelService panelService;


    /**
     * 显示系统所有看板信息
     *
     * @return
     */
    @GetMapping
    public SimplePageResponseVo<Object> showPanelList() {
        List<PanelVo> list = panelService.showPanelList();
        return SimplePageResponseVo.builder()
                .rows(list)
                .total(list.size())
                .build();
    }

    /**
     * 显示看板下的数据表
     *
     * @return
     */
    @GetMapping("/table")
    public SimplePageResponseVo<Object> showTablePanelByTableCn(String panelId) {
        List<Map<String, String>> list = panelService.showTablePanel(panelId);
        return SimplePageResponseVo.builder()
                .rows(list)
                .total(list.size())
                .build();
    }


    /**
     * 新建看板
     *
     * @param requestVo
     * @return
     */
    @PostMapping
    public SimpleResponseVo addPanel(@RequestBody SimpleRequestVo<String> requestVo) {
        String paneName = requestVo.getData();
        String loginId = requestVo.getLoginId();
        if (StringUtils.isBlank(paneName) && StringUtils.isBlank(loginId)) {
            throw new VerifyFailException("字段验证不通过!!");
        }
        if (paneName.length() > 30) {
            throw new VerifyFailException("面板字段长度超限!!");
        }
        boolean f = panelService.addPanel(paneName, loginId);
        if (f) {
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");
    }

    /**
     * 删除面板
     *
     * @return
     */
    @DeleteMapping
    public SimpleResponseVo deletePanel(@RequestBody SimpleRequestVo<String> requestVo) {
        String panelId = requestVo.getData();
        if (StringUtils.isBlank(panelId)) {
            throw new VerifyFailException("参数不正确!!!");
        }
        boolean f = panelService.deletePanel(panelId);
        if (f) {
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");
    }

    @PutMapping
    public SimpleResponseVo modifyPanelMenu(@RequestBody SimpleRequestVo<PanelVo> requestVo) {
        PanelVo panelVo = requestVo.getData();
        if (StringUtils.isBlank(panelVo.getMenuId())) {
            throw new VerifyFailException("菜单ID不能为空!!!");
        }
        boolean f = panelService.modifyPanelMenu(panelVo);
        if (f) {
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");

    }

    /**
     * 添加看板和表关系
     *
     * @param requestVo
     * @return
     */
    @PostMapping("/table")
    public SimpleResponseVo addPanelTable(@RequestBody SimpleRequestVo<Map<String, String>> requestVo) {
        Map<String, String> map = requestVo.getData();
        String tableId = map.get("tableId");
        String panelId = map.get("panelId");
        if (StringUtils.isBlank(tableId) && StringUtils.isBlank(panelId)) {
            throw new VerifyFailException("参数不能为空!!!");
        }
        boolean f = panelService.addPanelTable(tableId, panelId);
        if (f) {
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");
    }


    @GetMapping("/count")
    public SimplePageResponseVo<Object> getCount(String panelId, String tableNa, String tableCn) {
        if (StringUtils.isBlank(panelId)) {
            throw new VerifyFailException("参数不能为空!!!");
        }
        List<Map<String, Object>> maps = panelService.getCount(panelId, tableNa, tableCn);
        return SimplePageResponseVo.builder()
                .rows(maps)
                .total(maps.size())
                .build();
    }

}
